NYOJ_1278_Prototypes analyze_二叉树的种类

/*
Prototypes analyze
时间限制:1000 ms  |  内存限制:65535 KB
难度:2

描述

    ALpha Ceiling Manufacturers (ACM) is analyzing the properties of its new series of Incredibly Collapse-Proof Ceilings (ICPCs).  An ICPC consists of n layers of material, each with a different value of collapse resistance (measured as a positive integer). The analysis ACM wants to run will take the collapse-resistance values of the layers, store them in a binary search tree, and check whether the shape of this tree in any way correlates with the quality of the whole construction. Because, well, why should it not?  To be precise, ACM takes the collapse-resistance values for the layers, ordered from the top layer to the bottom layer,  and inserts them one-by-one into a tree. The rules for inserting a value v are:

    ? If the tree is empty, make v the root of the tree.

    ? If the tree is not empty, compare v with the root of the tree.

    ? If v is smaller, insert v into the left subtree of the root,

    ? otherwise insert v into the right subtree.

     

    ACM has a set of ceiling prototypes it wants to analyze by trying to collapse them. It wants to take each
     group of ceiling prototypes that have trees of the same shape and analyze them together. For example ,
      assume ACM is considering five ceiling prototypes with three layers each, as described by Sample Input 1 and shown
       in Figure C.1. Notice that the first prototype’s top layer has collapseresistance value 2, the middle layer has value 7,
        and the bottom layer has value 1. The second prototype has layers with collapse-resistance values of 3, 1, and 4 –
         and yet these two prototypes induce the same tree shape, so ACM will analyze them together. Given a set of prototypes,
          your task is to determine how many different tree shapes they induce.

     

输入
    The first line of the input contains one integers T, which is the nember of test cases (1<=T<=8).
    Each test case specifies :
    ● Line 1: two integers n (1 ≤ n ≤ 50), which is the number of ceiling prototypes to analyze,
    and k (1 ≤ k ≤ 20), which is the number of layers in each of the prototypes.
    ● The next n lines describe the ceiling prototypes. Each of these lines contains k distinct
    integers ( between 1 and 1e6, inclusive ) , which are the collapse-resistance values of the
    layers in a ceiling prototype, ordered from top to bottom.
输出
    For each test case generate a single line containing a single integer that is the number of different tree
    shapes.
样例输入

    1
    5 3
    2 7 1
    1 5 9
    3 1 4
    2 6 5
    9 7 3

样例输出

    4

*/

题意:求不同形状的二叉树有多少个?

思路:首先创建二叉排序树,按照先序遍历二叉排序树,最后去掉相同的。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Tree
{
	Tree *l;
	Tree *r;
	int data;//存放输入的数据 
	int count;//存放编号 
}*root,*p,*q;
int s[60][30];
int con,n,k;
Tree *c_Tree_root(int num,int x)
{
	p = (struct Tree *)malloc(sizeof(Tree));
	p->l=NULL;
	p->r=NULL;
	p->data = x;
	p->count = num;
	return p;
}
void Create_Tree(Tree *tree,int x)//创建二叉排序树 
{
	int num = tree->count;
	if(x >= tree->data)//如果当前值大于树根的值,放树的右孩子上。 
	{
		if(tree->r==NULL)//右孩子为NULL,放上面 
		{
			q = c_Tree_root(num*2+1,x); 
			tree->r = q; tree = q;
			return ;
		}
		else
		{ 
			Create_Tree(tree->r,x);//不为空,继续以右孩子为根查找 
		} 
	}
	else
	{
		if(tree->l==NULL)
		{
			q = c_Tree_root(num*2,x);
			tree->l=q; tree = q;
			return ;
		}
		else
		{ 
			Create_Tree(tree->l,x);
		} 
	}
}

void pre_Tree(Tree *tree,int i)//前序遍历 把编号存到数组中 
{
	if(tree!=NULL)
	{
		s[i][con++] = tree->count;
		pre_Tree(tree->l,i);
		pre_Tree(tree->r,i);
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int x,num;
		scanf("%d%d",&n,&k);
		int i,j,a;
		memset(s,0,sizeof(s));
		for(i=1;i<=n;i++)
		{
			scanf("%d",&x);
			root = c_Tree_root(1,x);//先创建一个根节点 
			for(j=1;j<k;j++)
			{
				scanf("%d",&x);
				Create_Tree(root,x); //创建二叉排序树 
			}
			con = 1;
			pre_Tree(root,i); 
		}
		int tem;
		for(i=1;i<n;i++)//去重 
		{
			for(j=i+1;j<=n;j++)
			{
				for(a=1,tem=0;a<=k;a++)
				{
					if(s[i][a]==s[j][a] && (s[i][a]+s[j][a])!=0)
					{
						tem ++ ;	
					}
				} 
				if(tem==k)
				{
					for(a=1;a<=k;a++)//找到相同的 把相同的标记为0 
					{
						s[j][a] = 0;
					}
				}
			} 
		} 
		int ans;
		int res = n;
		for(i=1;i<=n;i++) 
		{
			for(j=1,ans=0;j<=k;j++)
			{
				if(s[i][j]==0)
				{
					ans ++  ;
				}
			}
			if(ans==k)
			{
				res--;
			}
		}
		printf("%d\n",res);
		
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
x1y2 x2y3 x3y1-x1y3-x2y1-x3y2 是计算三角形面积的公式中的一部分。 在这个公式中,x1、x2、x3分别表示三角形的三个顶点的x坐标,y1、y2、y3分别表示三角形的三个顶点的y坐标。通过计算这个表达式的值,可以得到三角形的面积。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [TetraCluster:使用并行Java 2库的Java并行程序。 该程序在群集并行计算机上运行,​​以从给定的点集中找到...](https://download.csdn.net/download/weixin_42171208/18283141)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [线性代数有个题,求正交变换x=Qy,化二次型f(x1,x2,x3)=8x1x2+8x1x3+8x2x3为标准型求出特征值](https://blog.csdn.net/weixin_39956182/article/details/115882118)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [nyoj-67-三角形面积(S=(1/2)*(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2))](https://blog.csdn.net/weixin_30492601/article/details/99541033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值